using System;
using Core.GameObjects.Entities;

namespace Core.GameObjects.Items
{
    public class Money : BaseItem
    {
        private const int NUM_COINS_TO_WEIGHT_UNIT = 100;

        private int _Amount;

        //Overrides since the strength required changes as the amount changes
        public Byte StrengthRequired()
        {
            return (byte)(_Amount * 0.01);
        }

        public int Total()
        {
            return _Amount;
        }

        public Money(int ID, int Total) : base(ID, "Money", Money.GetWeight(Total), 1f, ItemType.Money)
        {
            _Amount = Total;
        }

        public static float GetWeight(int amount)
        {
            return (float)(amount / NUM_COINS_TO_WEIGHT_UNIT);
        }

        public float TotalWeight
        {
            get { return GetWeight(_Amount); }
        }

        //Called for things like buying items
        public void Subtract(int amount)
        {
            if (amount <= _Amount)
                _Amount -= amount;
        }

        //Parameter is declared as ref so we can modify it to let the calling code know
        //that we couldn't hold the full amount. This should never happen however
        //as the weight alone should limit what an entity can carry
        public void Add(ref int amount)
        {
            if (amount + _Amount <= int.MaxValue)
            {
                _Amount += amount;
            }
            else
            {
                amount = (int)(int.MaxValue - _Amount);
                _Amount = int.MaxValue;
            }
        }

        //Called for things like dropping on the ground etc
        //Added simply for making sense in the context for which it's used
        public void Drop(int amount)
        {
            Subtract(amount);
        }

        public void Give(ref int amount, ref Entity target)
        {
            if (amount <= _Amount)
            {
                //Have to get this working later
                //target.AddMoney(ref amount);
            }
        }
    }
}
